import { objHasElement } from './element';
import moment from 'moment';

// 获取无效的定时时间json方法
export const getDisabledTimelyFuncJson = ({
  startTimeFirst,
  endTimeFirst,
  startTimeSecond,
  endTimeSecond,
}) => {
  const formatHour = (time) => parseInt(moment(time).format('HH')),
    formatMinute = (time) => parseInt(moment(time).format('mm')),
    sHour1 = formatHour(startTimeFirst),
    sMinute1 = formatMinute(startTimeFirst),
    eHour1 = formatHour(endTimeFirst),
    eMinute1 = formatMinute(endTimeFirst),
    sHour2 = formatHour(startTimeSecond),
    sMinute2 = formatMinute(startTimeSecond),
    eHour2 = formatHour(endTimeSecond),
    eMinute2 = formatMinute(endTimeSecond),
    // 不能被定时器选中的小时数
    // 1、小于第一段申报时间的开始时间 2、在第一段结束到第二段开始的小时数之间 3、在第二段结束的小时数之后
    isIllLegalHour = (hour) => hour < sHour1 || (eHour1 < hour && hour < sHour2) || hour > eHour2,
    disabledHours = () =>
      Array.from({ length: 24 }, (_, index) => index).filter((hour) => isIllLegalHour(hour)),
    disabledMinutes = (selectedHour) => {
      // 如果小时数不合法则分钟都不能选择
      if (isIllLegalHour(selectedHour)) return Array.from({ length: 60 }, (_, index) => index);
      // 小于一段起始分钟数
      if (selectedHour === sHour1)
        return Array.from({ length: 60 }, (_, index) => index).filter(
          (minute) => minute < sMinute1,
        );
      // 大于一段结束分钟数
      if (selectedHour === sHour2)
        return Array.from({ length: 60 }, (_, index) => index).filter(
          (minute) => minute < sMinute2,
        );
      // 小于二段起始分钟数
      if (selectedHour === eHour1)
        return Array.from({ length: 60 }, (_, index) => index).filter(
          (minute) => minute > eMinute1,
        );
      // 大于二段结束分钟数
      if (selectedHour === eHour2)
        return Array.from({ length: 60 }, (_, index) => index).filter(
          (minute) => minute > eMinute2,
        );
    };
  return { disabledHours, disabledMinutes };
};

// 获取场站限额json，key按照场站id、时段id进行分类
export const getUnitPowerLimitInfo = (unitPowerLimitList) => {
  let result = {};
  unitPowerLimitList?.forEach(({ unitHourList, unitId }) => {
    let resultItem = {};
    unitHourList.forEach(
      (limitHourOrder) => (resultItem[limitHourOrder.hourOrder] = limitHourOrder),
    );
    result[unitId] = resultItem;
  });
  return result;
};

// 获取24时段的限额信息
export const get24HourLimitList = (powerLimitInfo, selectedUnitList, priceLimitInfo) => {
  if (!objHasElement(priceLimitInfo)) {
    return [];
  }
  let result = Array.from({ length: 24 }, (_, index) => index + 1).map((hourOrder) => ({
    buyLimit: 0.0,
    sellLimit: 0.0,
    upperPriceLimit: priceLimitInfo[hourOrder]?.priceUpperLimit,
    lowerPriceLimit: priceLimitInfo[hourOrder]?.priceLowerLimit,
  }));
  if (!objHasElement(powerLimitInfo) || !selectedUnitList) {
    return result;
  }

  // 初始化24时段的限额数组信息
  selectedUnitList?.forEach(({ unitId }) =>
    result.forEach((resultItem, index) => {
      let buyPowerLimit = 0,
        sellPowerLimit = 0;
      if (powerLimitInfo[unitId]) {
        buyPowerLimit = powerLimitInfo[unitId][index + 1].buyPowerLimit;
        sellPowerLimit = powerLimitInfo[unitId][index + 1].sellPowerLimit;
      }
      resultItem.buyLimit += buyPowerLimit;
      resultItem.sellLimit += sellPowerLimit;
    }),
  );
  return result;
};
